package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.config.PublisherAware;
import com.example.blog.domain.*;
import com.example.blog.dto.operationalLogs.OperationLogsDto;
import com.example.blog.dto.picture.AddPictureDto;
import com.example.blog.dto.picture.AdminSelectPictureDto;
import com.example.blog.dto.picture.DelPictureDto;
import com.example.blog.dto.picture.UpdatePictureDto;
import com.example.blog.dto.publicDto.PageDto;
import com.example.blog.dto.publicDto.QuerySearchDto;
import com.example.blog.event.publisher.LoggingApplicationEvent;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.note.Testaspect;
import com.example.blog.service.impl.OperationalLogsServiceImpl;
import com.example.blog.service.impl.PictureServiceImpl;
import com.example.blog.service.impl.UuIdServiceImpl;
import com.example.blog.utils.IpUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Transactional
@Validated
@RequestMapping("/picture")
public class PictureController extends BaseController {

    private final PictureServiceImpl pictureService;

    @Autowired
    PictureController(PictureServiceImpl pictureService) {
        this.pictureService = pictureService;
    }

    //分页获取全部图片
    @NoToken
    @Testaspect
    @LogInfo("分页获取全部图片")
    @GetMapping("/getAll/{index}")
    public Response pictureGetAll(@PathVariable("index") long index) {
        IPage page = new Page(index, 12);
        LambdaQueryWrapper<Picture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Picture::getStatue, 1);
        return new Response(Response.SUCCEED_CODE, "分页获取全部图片", pictureService.page(page, wrapper));
    }

    //分页获取指定类型图片
    @NoToken
    @LogInfo("分页获取指定类型")
    @PostMapping("/getAll/type")
    public Response pictureGetType(@RequestBody QuerySearchDto picture) {
        IPage page = new Page(picture.getIndex(), 12);
        LambdaQueryWrapper<Picture> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(picture.getCondition()),Picture::getType, picture.getCondition()).eq(Picture::getStatue, 1);
        return new Response(Response.SUCCEED_CODE, "分页获取指定类型", pictureService.page(page, wrapper).getRecords());
    }


    //Admin更新精美图片
    @Permissions("/picture/update")
    @LogInfo("Admin更新精美图片")
    @PostMapping("/update")
    public Response uploadFile(UpdatePictureDto pictureVo) {
        Users logSign = getLogSign();
        pictureVo.setUid(logSign.getUid());
        OperationLogsDto logsDto = new OperationLogsDto(
                0, IpUtil.getIpAddress(getHttpInfo()), "Admin更新精美图片", logSign,"更新精美图片id：".concat(String.valueOf(pictureVo.getId())));
        return new Response(Response.SUCCEED_CODE, "更新成功", pictureService.updatePictureImg(pictureVo,logsDto));
    }

    //新增图片
    @Permissions("/picture/insert/into")
    @LogInfo("Admin新增精选图片")
    @PostMapping("/insert/into")
    public Response pictureInsert(AddPictureDto picture) {
        OperationLogsDto logsDto = new OperationLogsDto(
                1, IpUtil.getIpAddress(getHttpInfo()), "Admin更新精美图片", getLogSign(), null);
        return new Response(Response.SUCCEED_CODE, "新增成功", pictureService.addPictureImg(new MultipartFile[]{picture.getFile()},picture,logsDto) );
    }

    //删除图片
    @LogInfo("Admin删除精选图片")
    @Permissions("/picture/delete")
    @PostMapping("/delete")
    public Response pictureDelete(@RequestBody DelPictureDto delPictureVo) {
        OperationLogsDto logsDto = new OperationLogsDto(
                -1, IpUtil.getIpAddress(getHttpInfo()),"Admin删除精选图片",getLogSign(), null);
        return new Response(Response.SUCCEED_CODE, "删除成功", pictureService.delPictureImg(delPictureVo,logsDto));
    }


    //获取所有图片的分类
    @NoToken
    @LogInfo("获取所有图片的分类")
    @GetMapping("/getType")
    public Response pictureType() {
        QueryWrapper<Picture> wrapper = new QueryWrapper<>();
        wrapper.select("type");
        wrapper.groupBy("type");
        List<Picture> list = pictureService.list(wrapper);
        List<String> typeList = new ArrayList<>();
        list.forEach(s->{
            if(s.getType().contains(",")){
                String[] split = s.getType().split(",");
                List<String> collect = Arrays.stream(split).filter(str -> !Objects.equals(str, "")).collect(Collectors.toList());
                typeList.addAll(collect);
            }else{
                typeList.add(s.getType());
            }
        });
        List<String> collect = typeList.stream().distinct().collect(Collectors.toList());
        return new Response(Response.SUCCEED_CODE, "获取所有图片的分类",collect );
    }

    //Admin分页获取全部图片
    @Permissions("/picture/getAll/admin")
    @LogInfo("Admin查询精美图片")
    @PostMapping("/getAll/admin")
    public Response pictureAdminGetAll(@RequestBody AdminSelectPictureDto querySearchVo) {
        IPage<Picture> page = new Page<>(querySearchVo.getIndex(), querySearchVo.getPage());
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(Objects.nonNull(querySearchVo.getDateTime()),Picture::getTime,querySearchVo.getDateTime())
                .eq(querySearchVo.getTypes() != 2, Picture::getStatue, querySearchVo.getTypes()).orderByDesc(Picture::getTime);
        if (querySearchVo.getCondition() != null) {
            queryWrapper.and(wrapper -> wrapper.like(Picture::getType, querySearchVo.getCondition())
                    .or().like(Picture::getDescription, querySearchVo.getCondition())
                    .or().like(Picture::getUid, querySearchVo.getCondition())
                    .or().like(Picture::getId, querySearchVo.getCondition()));
        }
        return new Response(Response.SUCCEED_CODE, "Admin查询精美图片", pictureService.page(page, queryWrapper));
    }

}
